package com.woniuxy.utils;

import com.nimbusds.jose.*;
import com.nimbusds.jose.crypto.MACSigner;

import java.text.ParseException;
import java.util.Map;

public class JWTUtil {
    public static String createJWT(Map map) throws JOSEException {
        //生成jwt头部
        JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.HS256)
                .type(JOSEObjectType.JWT).build();

        //生成jwt载荷

        Payload payload = new Payload(map);

        //生成jwt签名
        JWSObject jwsObject = new JWSObject(jwsHeader,payload);
        String secretKey = "0123456789abcdefghijklmnopqrstuvwxyz";
        MACSigner signer = new MACSigner(secretKey);
        jwsObject.sign(signer);
        String jwt = jwsObject.serialize();
        return jwt;
    }
    public static Map getJWTUserInfo(String jwt) throws ParseException {
        JWSObject jwsObject = JWSObject.parse(jwt);
        Payload payload = jwsObject.getPayload();
        return payload.toJSONObject();
    }

}
